<html lang="zh-CN"><head><meta charset="UTF-8"><style>.nodata  main {width:1000px;margin: auto;}</style></head><body class="nodata " style=""><div class="main_father clearfix d-flex justify-content-center " style="height:100%;"> <div class="container clearfix " id="mainBox"><main><div class="blog-content-box">
<div class="article-header-box">
<div class="article-header">
<div class="article-title-box">
<h1 class="title-article" id="articleContentId">(C卷,100分)- 找座位（Java & JS & Python & C）</h1>
</div>
</div>
</div>
<div id="blogHuaweiyunAdvert"></div>

        <div id="article_content" class="article_content clearfix">
        <link rel="stylesheet" href="https://csdnimg.cn/release/blogv2/dist/mdeditor/css/editerView/kdoc_html_views-1a98987dfd.css">
        <link rel="stylesheet" href="https://csdnimg.cn/release/blogv2/dist/mdeditor/css/editerView/ck_htmledit_views-044f2cf1dc.css">
                <div id="content_views" class="htmledit_views">
                    <p id="main-toc"><strong>目录</strong></p> 
<p id="main-toc-toc" style="margin-left:80px;"><a href="#main-toc" rel="nofollow">题目描述</a></p> 
<p id="%E8%BE%93%E5%85%A5%E6%8F%8F%E8%BF%B0-toc" style="margin-left:80px;"><a href="#%E8%BE%93%E5%85%A5%E6%8F%8F%E8%BF%B0" rel="nofollow">输入描述</a></p> 
<p id="%E8%BE%93%E5%87%BA%E6%8F%8F%E8%BF%B0-toc" style="margin-left:80px;"><a href="#%E8%BE%93%E5%87%BA%E6%8F%8F%E8%BF%B0" rel="nofollow">输出描述</a></p> 
<p id="%E7%94%A8%E4%BE%8B-toc" style="margin-left:80px;"><a href="#%E7%94%A8%E4%BE%8B" rel="nofollow">用例</a></p> 
<p id="%E9%A2%98%E7%9B%AE%E8%A7%A3%E6%9E%90-toc" style="margin-left:80px;"><a href="#%E9%A2%98%E7%9B%AE%E8%A7%A3%E6%9E%90" rel="nofollow">题目解析</a></p> 
<p id="%E7%AE%97%E6%B3%95%E6%BA%90%E7%A0%81-toc" style="margin-left:80px;"><a href="#%E7%AE%97%E6%B3%95%E6%BA%90%E7%A0%81" rel="nofollow">JavaScript算法源码</a></p> 
<p id="Java%E7%AE%97%E6%B3%95%E6%BA%90%E7%A0%81-toc" style="margin-left:80px;"><a href="#Java%E7%AE%97%E6%B3%95%E6%BA%90%E7%A0%81" rel="nofollow">Java算法源码</a></p> 
<p id="Python%E7%AE%97%E6%B3%95%E6%BA%90%E7%A0%81-toc" style="margin-left:80px;"><a href="#Python%E7%AE%97%E6%B3%95%E6%BA%90%E7%A0%81" rel="nofollow">Python算法源码</a></p> 
<p id="C%E7%AE%97%E6%B3%95%E6%BA%90%E7%A0%81-toc" style="margin-left:80px;"><a href="#C%E7%AE%97%E6%B3%95%E6%BA%90%E7%A0%81" rel="nofollow">C算法源码</a></p> 
<hr id="hr-toc" /> 
<p></p> 
<h4 id="main-toc">题目描述</h4> 
<p>在一个大型体育场内举办了一场大型活动&#xff0c;由于疫情防控的需要&#xff0c;要求每位观众的必须间隔至少一个空位才允许落座。</p> 
<p>现在给出一排观众座位分布图&#xff0c;座位中存在已落座的观众&#xff0c;请计算出&#xff0c;在不移动现有观众座位的情况下&#xff0c;最多还能坐下多少名观众。</p> 
<p></p> 
<h4 id="%E8%BE%93%E5%85%A5%E6%8F%8F%E8%BF%B0">输入描述</h4> 
<p>一个数组&#xff0c;用来标识某一排座位中&#xff0c;每个座位是否已经坐人。0表示该座位没有坐人&#xff0c;1表示该座位已经坐人。</p> 
<ul><li>1 ≤ 数组长度 ≤ 10000</li></ul> 
<p></p> 
<h4 id="%E8%BE%93%E5%87%BA%E6%8F%8F%E8%BF%B0">输出描述</h4> 
<p>整数&#xff0c;在不移动现有观众座位的情况下&#xff0c;最多还能坐下多少名观众。</p> 
<p></p> 
<h4 id="%E7%94%A8%E4%BE%8B">用例</h4> 
<table border="1" cellpadding="1" cellspacing="1" style="width:500px;"><tbody><tr><td style="width:86px;">输入</td><td style="width:412px;">10001</td></tr><tr><td style="width:86px;">输出</td><td style="width:412px;">1</td></tr><tr><td style="width:86px;">说明</td><td style="width:412px;">无</td></tr></tbody></table> 
<table border="1" cellpadding="1" cellspacing="1" style="width:500px;"><tbody><tr><td style="width:86px;">输入</td><td style="width:412px;">0101</td></tr><tr><td style="width:86px;">输出</td><td style="width:412px;">0</td></tr><tr><td style="width:86px;">说明</td><td style="width:412px;">无</td></tr></tbody></table> 
<p></p> 
<h4 id="%E9%A2%98%E7%9B%AE%E8%A7%A3%E6%9E%90">题目解析</h4> 
<p>本题和<a href="https://blog.csdn.net/qfc_128220/article/details/130764903?spm&#61;1001.2014.3001.5501" title="华为OD机试 - 座位调整&#xff08;Java &amp; JS &amp; Python &amp; C&#xff09;_伏城之外的博客-CSDN博客">华为OD机试 - 座位调整&#xff08;Java &amp; JS &amp; Python &amp; C&#xff09;_伏城之外的博客-CSDN博客</a>类似&#xff0c;解析可以看该博客。</p> 
<p></p> 
<h4 id="%E7%AE%97%E6%B3%95%E6%BA%90%E7%A0%81">JavaScript算法源码</h4> 
<pre><code class="language-javascript">const rl &#61; require(&#34;readline&#34;).createInterface({ input: process.stdin });
var iter &#61; rl[Symbol.asyncIterator]();
const readline &#61; async () &#61;&gt; (await iter.next()).value;

void (async function () {
  const desk &#61; [...(await readline())];
  console.log(getResult(desk));
})();

function getResult(desk) {
  let ans &#61; 0;

  for (let i &#61; 0; i &lt; desk.length; i&#43;&#43;) {
    if (desk[i] &#61;&#61; &#34;0&#34;) {
      const isLeftEmpty &#61; i &#61;&#61; 0 || desk[i - 1] &#61;&#61; &#34;0&#34;;
      const isRightEmpty &#61; i &#61;&#61; desk.length - 1 || desk[i &#43; 1] &#61;&#61; &#34;0&#34;;

      if (isLeftEmpty &amp;&amp; isRightEmpty) {
        ans&#43;&#43;;
        desk[i] &#61; &#34;1&#34;;
        i&#43;&#43;;
      }
    }
  }

  return ans;
}
</code></pre> 
<p> </p> 
<h4 id="Java%E7%AE%97%E6%B3%95%E6%BA%90%E7%A0%81">Java算法源码</h4> 
<pre><code class="language-java">import java.util.Scanner;

public class Main {
  public static void main(String[] args) {
    Scanner sc &#61; new Scanner(System.in);
    System.out.println(getResult(sc.nextLine().toCharArray()));
  }

  public static int getResult(char[] desk) {
    int ans &#61; 0;

    for (int i &#61; 0; i &lt; desk.length; i&#43;&#43;) {
      if (desk[i] &#61;&#61; &#39;0&#39;) {
        boolean isLeftEmpty &#61; i &#61;&#61; 0 || desk[i - 1] &#61;&#61; &#39;0&#39;;
        boolean isRightEmpty &#61; i &#61;&#61; desk.length - 1 || desk[i &#43; 1] &#61;&#61; &#39;0&#39;;
        if (isLeftEmpty &amp;&amp; isRightEmpty) {
          ans&#43;&#43;;
          desk[i] &#61; &#39;1&#39;;
          i&#43;&#43;;
        }
      }
    }

    return ans;
  }
}
</code></pre> 
<p> </p> 
<h4 id="Python%E7%AE%97%E6%B3%95%E6%BA%90%E7%A0%81">Python算法源码</h4> 
<pre><code class="language-python"># 输入获取
desk &#61; list(input())


# 算法入口
def getResult():
    ans &#61; 0

    i &#61; 0
    while i &lt; len(desk):
        if desk[i] &#61;&#61; &#39;0&#39;:
            isLeftEmpty &#61; i &#61;&#61; 0 or desk[i - 1] &#61;&#61; &#39;0&#39;
            isRightEmpty &#61; i &#61;&#61; len(desk) - 1 or desk[i &#43; 1] &#61;&#61; &#39;0&#39;

            if isLeftEmpty and isRightEmpty:
                ans &#43;&#61; 1
                desk[i] &#61; &#39;1&#39;
                i &#43;&#61; 1
        i &#43;&#61; 1

    return ans


# 算法调用
print(getResult())</code></pre> 
<p> </p> 
<h4 id="C%E7%AE%97%E6%B3%95%E6%BA%90%E7%A0%81">C算法源码</h4> 
<pre><code class="language-cpp">#include &lt;stdio.h&gt;
#include &lt;string.h&gt;

#define MAX_SIZE 20000

int getResult(char *desk);

int main() {
    char desk[MAX_SIZE];
    gets(desk);

    printf(&#34;%d\n&#34;, getResult(desk));

    return 0;
}

int getResult(char *desk) {
    unsigned long long desk_size &#61; strlen(desk);

    int ans &#61; 0;
    for (int i &#61; 0; i &lt; desk_size; i&#43;&#43;) {
        if (desk[i] &#61;&#61; &#39;0&#39;) {
            int isLeftEmpty &#61; i &#61;&#61; 0 || desk[i - 1] &#61;&#61; &#39;0&#39;;
            int isRightEmpty &#61; i &#61;&#61; desk_size - 1 || desk[i &#43; 1] &#61;&#61; &#39;0&#39;;

            if (isLeftEmpty &amp;&amp; isRightEmpty) {
                ans&#43;&#43;;
                desk[i] &#61; &#39;1&#39;;
                i&#43;&#43;;
            }
        }
    }

    return ans;
}</code></pre> 
<p></p>
                </div>
        </div>
        <div id="treeSkill"></div>
        <div id="blogExtensionBox" style="width:400px;margin:auto;margin-top:12px" class="blog-extension-box"></div>
    <script>
  $(function() {
    setTimeout(function () {
      var mathcodeList = document.querySelectorAll('.htmledit_views img.mathcode');
      if (mathcodeList.length > 0) {
        for (let i = 0; i < mathcodeList.length; i++) {
          if (mathcodeList[i].naturalWidth === 0 || mathcodeList[i].naturalHeight === 0) {
            var alt = mathcodeList[i].alt;
            alt = '\\(' + alt + '\\)';
            var curSpan = $('<span class="img-codecogs"></span>');
            curSpan.text(alt);
            $(mathcodeList[i]).before(curSpan);
            $(mathcodeList[i]).remove();
          }
        }
        MathJax.Hub.Queue(["Typeset",MathJax.Hub]);
      }
    }, 1000)
  });
</script>
</div></html>